package main

import "fmt"

/*建造者模式*/
func main() {
	shop := &Shop{new(Commodity)}
	fmt.Println(shop.Purchase("红", "苹果", 12))
	fmt.Println(shop.Purchase("橙", "乒乓球", 2))
}

type Shop struct {
	factory Factory
}

func (f *Shop) Purchase(color, name string, price float64) *Commodity {
	return f.factory.SetColor(color).SetName(name).SetPrice(price).Produce()
}

type Factory interface {
	Produce() *Commodity
	SetColor(color string) *Commodity
	SetPrice(price float64) *Commodity
	SetName(name string) *Commodity
}

/*商品*/
type Commodity struct {
	Color string
	Price float64
	Name  string
}

func (f *Commodity) Produce() *Commodity {
	return f
}

func (f *Commodity) SetColor(color string) *Commodity {
	f.Color = color
	return f
}
func (f *Commodity) SetPrice(price float64) *Commodity {
	f.Price = price
	return f
}
func (f *Commodity) SetName(name string) *Commodity {
	f.Name = name
	return f
}
